package controllers

import (
	"net/http"

	"gitlab.yunzhen-data.com/C-Chain/titan/bytesutils"
	"gitlab.yunzhen-data.com/C-Chain/titan/httpengine"

	"gitlab.yunzhen-data.com/C-Chain/titan-template/configurations"
)

type HealthController struct {
}

func NewHealthController() *HealthController {
	return &HealthController{}
}

func (receiver *HealthController) Inject() string {
	return bytesutils.EmptyString
}

func (receiver *HealthController) GroupMapping() string {
	return "/"
}

func (receiver *HealthController) Mapping() []httpengine.MappingUnit {
	return []httpengine.MappingUnit{
		{
			HTTPMethod:  http.MethodGet,
			PathPattern: "/health",
			Impl:        receiver.Health,
			Produce:     nil,
		},
	}
}

func (receiver *HealthController) Produce() httpengine.ResponseProcessor {
	return new(httpengine.BizJSONResponseProcessor)
}

/**
 * @api {get} /health 健康检查
 * @apiVersion 0.1.0
 * @apiGroup Health
 * @apiSuccess {String} build_date 系统构建时间
 * @apiSuccess {String} git_commit_id 系统构建的Commit ID
 * @apiSuccess {String} release_version 发布的版本
 */
func (receiver *HealthController) Health(request httpengine.Request, response httpengine.Response) (result any, err error) {
	type BuildVersion struct {
		BuildDate      string `json:"build_date"`
		GitCommitID    string `json:"git_commit_id"`
		ReleaseVersion string `json:"release_version"`
	}
	return &BuildVersion{
		BuildDate:      configurations.BuildDate,
		GitCommitID:    configurations.GitCommitID,
		ReleaseVersion: configurations.ReleaseVersion,
	}, nil
}
